package com.demo.gereric;

public class Food {
	class Fruit extends Food {
	}


	class Apple extends Fruit {
	}


	class Banana extends Fruit {
	}


	class Plate<T extends Fruit> {
		private T item;


		public Plate(T item) {
			this.item = item;
		}


		public T getItem() {
			return item;
		}


		public void setItem(T item) {
			this.item = item;
		}
	}

	public void testUpperBoundedBoundedGeneric() {
		Plate<? extends Fruit> p = new Plate<>(new Apple());

		// <? extends Fruit> 会使往盘子里放东西的 set( ) 方法失效
//		 p.setItem(new Fruit());
		// p.setItem(new Apple());

		Fruit fruit = p.getItem();
		Food food = p.getItem();
	}

	public void testLowerBoundedBoundedGeneric() {
		Plate<? super Fruit> p = new Plate<>(new Fruit());

		p.setItem(new Fruit());
		p.setItem(new Apple());

		// 会报错，只有所有类的基类 Object 可以装下
//		Apple apple = p.getItem();
		Object o = p.getItem();
	}
}
